/**
 *  Class: $.Scrollbar
 *  Scrollbar is used when the content on the application is bigger than the screen
*/

(function($) {

	//ScrollBar Main Method
	$.fn.sfScrollbar2 = function(options, param) 
	{
		if (!$(this).data('info')) {

			$(this).data('info',{
						size : options.size,
						type : options.type,
						pages : options.pages
					});
				
			//Component Scructure
			var contents = [	
				'<div class="scrollBar_container">',				
					'<div class="scrollBar_barTop"></div>',
					'<div class="scrollBar_barMiddle">',
						'<div class="scrollBar_barImg"></div>',
					'</div>',
					'<div class="scrollBar_barBottom"></div>',
				'</div>'
			].join('');
			
			//Attach the Component Structure to the HTML element
			$(this).append(contents);
		}
		                  
		//Component Behavior
		if (typeof options == 'object') 
		{
			var info = new Array();
			var tType = '';
			tType = options.type;
			
			info.size 	 = options.size;
			info.type 		 = tType.toLowerCase()
			info.pages 		 = options.pages;
						
			//Save Data
			$(this).data('info',info);	
			
			if (info.type == 'vertical')
				createVerticalScroll(this, options);
			else if (info.type == 'horizontal')
				createHorizontalScroll(this, options);
			else
				createVerticalScroll(this, options);
		}
		else if (typeof options == 'string') 
		{
			options = options.toLowerCase();
	
			switch (options) {
				case 'go':break;
				case 'show': $(this).show(); break;
				case 'hide': $(this).hide(); break;
				case 'move': move(this,param); break
				default: alert('Type error!'); break;
			}
		 }
		
	    return this;
	};
	
	/**
	 * createVerticalScroll
	 * Create the vertical scroll structure. 
	 */
	function createVerticalScroll(self, options) 
	{	alert(" ::::: VERTICAL SCROLL ::::: ");
		
		var info = new Array();
		info = $(self).data('info');
				
		//Height
		$(self).children().children(':nth-child(2)').css('height',info.size);

	}
	
	
	/**
	 * createHorizontalScroll
	 * Create the horizontal scroll structure. 
	 */
	function createHorizontalScroll(self, options) 
	{	alert(" ::::: Horizontal SCROLL ::::: ");
		
		var info = new Array();
		info = $(self).data('info');
				
		//Width of Button Img		
		var btnWidth = Number($(self).children().children(':nth-child(1)').css('width').replace('px',''));
		
		//Scrollbar Total Width (user provided) - 2* btnWidth
		var scrollWidth = info.size - (2*btnWidth);
				
		//Width
		$(self).children().children(':nth-child(2)')
			.removeClass('scrollBar_barMiddle')
			.addClass('scrollBar_barMiddle_h');
				
		//Orientation
		$(self).children().children(':nth-child(1)').css('float','left');
		$(self).children().children(':nth-child(2)').css('float','left');
		$(self).children().children(':nth-child(3)').css('float','right');
		
		//Change css rules
		$(self).children().children(':nth-child(1)')
			.removeClass('scrollBar_barTop')
			.addClass('scrollBar_barTop_h');
			
		$(self).children().children(':nth-child(2)').children()
			.removeClass('scrollBar_barImg')
			.addClass('scrollBar_barImg_h');
		
		$(self).children().children(':nth-child(2)').css('width',scrollWidth);
		
		$(self).children().children(':nth-child(3)')
			.removeClass('scrollBar_barTop')
			.addClass('scrollBar_barBottom_h');

	}
	
	/**
	* move
	* Move the page
	*/
	function move(self, param)
	{	alert(" ::::: Move Scroll Page ::::: ");
		
		var currentPage = param;
		var info = new Array();
		info = $(self).data('info');
			
		if (info.type == 'vertical')
			var barImgHeight = 	Number($(self).children().children(':nth-child(2)').children().height());
		else {
			var barImgHeight = 	Number($(self).children().children(':nth-child(2)').children().width());
			barImgHeight = barImgHeight + ( 2 * Number($(self).children().children(':nth-child(1)').css('width').replace('px','')));
		}

		var margin = Math.floor((info.size-barImgHeight) / (info.pages-1));
		var marginTop = 0;			
		
		if (currentPage < (info.pages-1)) {
			marginTop = Number(margin * currentPage);
			
			if (info.type == 'vertical')
				$(self).children().children(':nth-child(2)').children().css('margin-top',marginTop);
			else
				$(self).children().children(':nth-child(2)').children().css('margin-left',marginTop);
				
		} else if (currentPage == (info.pages-1)) {
			if (info.type == 'vertical')
				$(self).children().children(':nth-child(2)').children().css('margin-top',(info.size-barImgHeight));
			else {
			alert((info.size-barImgHeight));
				$(self).children().children(':nth-child(2)').children().css('margin-left',(info.size-barImgHeight));
			}
		} else {
			if (info.type == 'vertical')
				$(self).children().children(':nth-child(2)').children().css('margin-top','0px');
			else
				$(self).children().children(':nth-child(2)').children().css('margin-left','0px');
		}
	};
})(jQuery);
